<f:view xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui"
	xmlns:pm="http://primefaces.org/mobile"
	contentType="text/html"
    renderKitId="PRIMEFACES_MOBILE">

    <pm:page title="Chat">
        
        <f:facet name="postinit">
            <script type="text/javascript">
                //<![CDATA[
                    function handleMessage(data) {
                        var chatContent = $(PrimeFaces.escapeClientId('chatForm:content'));
                        chatContent.append(data + '<br />');

                        //keep scroll
                        chatContent.scrollTop(chatContent.height());
                    }
                //]]>
            </script>
        </f:facet>

        <!-- Main View -->
        <pm:view id="main" swatch="a">
            <pm:header title="Chat">
            </pm:header>

            <pm:content>
                <h:form id="chatForm">
                    
                    <p:outputPanel id="container">
                        <h:panelGroup rendered="#{chatView.loggedIn}" >
                            <p:outputPanel id="content" layout="block" style="width:100%;height:200px;overflow:auto"/>

                            <p:separator />

                            <p:inputText value="#{chatView.globalMessage}" styleClass="messageInput" />
                            <p:commandButton value="Send" actionListener="#{chatView.sendGlobal}" 
                                     oncomplete="$('.messageInput').val('')"/>
                        </h:panelGroup>
                    
                        <h:panelGroup rendered="#{not chatView.loggedIn}" >
                            <p:inputText id="username" value="#{chatView.username}" />
                            <p:watermark for="username" value="Username" />
                            
                            <p:separator />

                            <p:commandButton value="Login" icon="refresh" update="container"
                                              actionListener="#{chatView.login}" />
                        </h:panelGroup>
                    </p:outputPanel>
                            
                </h:form>
                
                <p:socket onMessage="handleMessage" channel="/chat" widgetVar="subscriber"/>
                
            </pm:content>

        </pm:view>

    </pm:page>
    
</f:view>